package com.imachen.ymcc.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.imachen.ymcc.domain.UserBaseInfo;
import com.imachen.ymcc.domain.UserGrowLog;
import com.imachen.ymcc.dto.GrowScoreDto;
import com.imachen.ymcc.enums.GlobalExceptionCode;
import com.imachen.ymcc.mapper.UserBaseInfoMapper;
import com.imachen.ymcc.service.IUserBaseInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.imachen.ymcc.service.IUserGrowLogService;
import com.imachen.ymcc.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 会员基本信息 服务实现类
 * </p>
 *
 * @author imachen
 * @since 2022-09-23
 */
@Service
public class UserBaseInfoServiceImpl extends ServiceImpl<UserBaseInfoMapper, UserBaseInfo> implements IUserBaseInfoService {

    @Autowired
    private IUserGrowLogService userGrowLogService;

    /**
     * 推荐新人注册送成长值
     * @param userId
     */
    @Override
    public void giveGrowScore(Long userId) {
        // 添加成长值表
        saveUserBaseInfo(userId,10);
        // 添加成长值日志t_user_grow_log
        saveUserGrowLog(userId,10,"推荐新人注册送成长值");
    }


    /**
     * 充值送成长值  充值按照充值⾦额 5%赠送成⻓值
     * @param growScoreDto
     */
    @Override
    public void rechargeGiveGrowScore(GrowScoreDto growScoreDto) {
        Long userId = growScoreDto.getUserId();
        BigDecimal rechargePrice = growScoreDto.getRechargePrice();
        // 获取成长值
        Integer growScore = getGrowScore(rechargePrice, 0.05);
        // 添加成长值表t_user_base_info
        saveUserBaseInfo(userId,growScore);
        // 添加成长值日志t_user_grow_log
        saveUserGrowLog(userId,growScore,"充值金额送成长值");
    }


    /**
     * 购买课程送成长值
     * @param growScoreDto
     */
    @Override
    public void buyCourseGiveGrowScore(GrowScoreDto growScoreDto) {
        Long userId = growScoreDto.getUserId();
        BigDecimal coursePrice = growScoreDto.getCoursePrice();
        // 购买课程,推荐人按照购买课程⾦额3%赠送成长值
        // 查询推荐人
        UserBaseInfo userBaseInfo = selectById(userId);
        Long referId = userBaseInfo.getReferId();// 推荐人id
        // 获取成长值
        Integer growScore = getGrowScore(coursePrice, 0.03);
        // 添加成长值表t_user_base_info
        saveUserBaseInfo(referId,growScore);
        // 添加成长值日志t_user_grow_log
        saveUserGrowLog(referId,growScore,"推荐的人购买课程赠送成长值");
    }



    /* 添加成长值日志 */
    private void saveUserGrowLog(Long userId, Integer growScore,String fromReason) {
        UserGrowLog userGrowLog = new UserGrowLog();
        userGrowLog.setCreateTime(new Date());
        userGrowLog.setUserId(userId);
        userGrowLog.setFromReason(fromReason);
        userGrowLog.setScore(growScore);
        userGrowLogService.insert(userGrowLog);

    }

    /* 添加成长值信息 */
    private void saveUserBaseInfo(Long userId, Integer growScore) {
        UserBaseInfo userBaseInfo = selectById(userId);
        AssertUtil.isNotNull(userBaseInfo, GlobalExceptionCode.USER_IS_NULL_ERROR);
        int grow = userBaseInfo.getGrowScore() + growScore;
        userBaseInfo.setGrowScore(grow);
        userBaseInfo.setLevel(getLevel(grow));
        updateById(userBaseInfo);
    }


    /* 获取成长值 */
    private Integer getGrowScore(BigDecimal price, double discount) {
        BigDecimal bigDecimal = price.multiply(BigDecimal.valueOf(discount));
        int growScore = bigDecimal.setScale(0, BigDecimal.ROUND_DOWN).intValue();
        return growScore;
    }

    /* 判断成长值等级 */
    public Integer getLevel(Integer growScore) {
        if (growScore >= 0 && growScore<=99) {
            return 0;
        } else if (growScore >= 100 && growScore<=299) {
            return 1;
        } else if (growScore >= 300 && growScore<=599) {
            return 2;
        } else if (growScore >= 600 && growScore<=999) {
            return 3;
        } else if (growScore >= 1000 && growScore<1499) {
            return 4;
        } else if (growScore >= 1500 && growScore<=2499) {
            return 5;
        } else {
            return 6;
        }
    }
}
